// 出库管理 API 接口
const API_BASE_URL = 'http://localhost:3000';

export interface OutboundRecord {
  id?: number;
  code: string;
  flag: boolean;
  type: string;
  name: string;
  total: number;
  issued: number;
  time: string;
  outtime: string;
}

// 获取所有出库记录
export const getOutboundRecords = async (): Promise<OutboundRecord[]> => {
  const response = await fetch(`${API_BASE_URL}/api/out`);
  if (!response.ok) {
    throw new Error('获取出库记录失败');
  }
  return response.json();
};

// 根据ID获取出库记录
export const getOutboundRecord = async (id: number): Promise<OutboundRecord> => {
  const response = await fetch(`${API_BASE_URL}/api/out/${id}`);
  if (!response.ok) {
    throw new Error('获取出库记录失败');
  }
  return response.json();
};

// 添加出库记录
export const addOutboundRecord = async (record: Omit<OutboundRecord, 'id'>): Promise<OutboundRecord> => {
  const response = await fetch(`${API_BASE_URL}/api/out`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(record),
  });
  if (!response.ok) {
    throw new Error('添加出库记录失败');
  }
  return response.json();
};

// 更新出库记录
export const updateOutboundRecord = async (id: number, record: Partial<OutboundRecord>): Promise<OutboundRecord> => {
  const response = await fetch(`${API_BASE_URL}/api/out/${id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(record),
  });
  if (!response.ok) {
    throw new Error('更新出库记录失败');
  }
  return response.json();
};

// 删除出库记录
export const deleteOutboundRecord = async (id: number): Promise<void> => {
  const response = await fetch(`${API_BASE_URL}/api/out/${id}`, {
    method: 'DELETE',
  });
  if (!response.ok) {
    throw new Error('删除出库记录失败');
  }
};
